package com.ebring.shareEducation.controller;

import com.ebring.shareEducation.bean.ConditionRole;
import com.ebring.shareEducation.bean.Role;
import com.ebring.shareEducation.bean.User;
import com.ebring.shareEducation.service.RoleService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.locks.Condition;

@RestController
@CrossOrigin(origins = "http://wangliang.vipgz1.idcfengye.com", maxAge = 3600)
@Api(value = "角色管理" ,tags = {"角色管理接口"})
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询所有的角色（分页查询）
     */
    @GetMapping("/role/list")
    @ApiOperation(value="查询所有的角色（分页查询）", notes="查询所有的角色（分页查询）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "总页数", required = true, dataType = "Integer"),
    })
    public RspMsg<List<Role>> selectRole(Integer pageNum, Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        return roleService.selectRole();
    }


    /**
     * 根据角色id查询角色的下级角色
     */
    @GetMapping("/role/selectCRByUserRole")
    @ApiOperation(value="根据角色id查询角色的下级角色", notes="根据角色id查询角色的下级角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String"),
    })
    public RspMsg<List<ConditionRole>> selectCRByUserRole(String roleId) {
        return roleService.selectCRByUserRole(roleId);
    }

    /**
     * 通过用户id查询角色
     */
    @PostMapping("/role/selectRoleByUserId")
    @ApiOperation(value="通过用户id查询角色", notes="通过用户id查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
    })
    public RspMsg<List<Role>> selectRoleByUserId(String userId) {
        return roleService.selectRoleByUserId(userId);
    }

    /**
     * 新增角色
     */
    @PostMapping("/role/insert")
    @ApiOperation(value="新增角色", notes="新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "Description", value = "描述", required = true, dataType = "String"),


    })
    public RspMsg<Role> insertRole(String name, String Description) {
        Role role = new Role();
        role.setRoleId(UuidUtils.getNumberId(4));
        role.setName(name);
        role.setDescription(Description);
        return roleService.insetRole(role);

    }

    /**
     * 修改角色
     */
    @PostMapping("/role/update")
    @ApiOperation(value="修改角色", notes="修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Role", value = "角色对象", required = true, dataType = "Role"),
    })
    public RspMsg<Role> updateRole(Role role) {
        return roleService.updateRole(role);
    }

    /**
     * 角色菜单模糊查询
     *
     * @param roleId
     * @param name
     * @return
     */
    @GetMapping("/role/select")
    @ApiOperation(value="角色模糊查询", notes="角色模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "总页数", required = true, dataType = "Integer")
    })
    public RspMsg<List<Role>> select(String roleId, String name, Integer pageNum, Integer pageSize) {

        if (pageNum == null) {
            pageNum = 1;

        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        return roleService.select(roleId, name);
    }
    /**
     * 删除角色
     *
     *
     * @param roleId 角色id
     *
     */
    @GetMapping("/role/delete")
    @ApiOperation(value="删除角色", notes="删除角色")
    @ApiImplicitParam(paramType="query",name = "roleId", value = "角色id", required = true, dataType = "String[]")
    public RspMsg<Role> DeleteUsers(String roleId) {

        return roleService.DeleteRole(roleId);

    }


}
